//
//  XQLargeCardController.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit
import MJRefresh

class XQLargeCardController: XQBaseCardController {
    
    var refreshHandle: (() -> Void)?
    
    var applyHandle: (() -> Void)?
    
    private lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        
        scrollView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.refreshHandle?()
        })
        
        return scrollView
    }()
    
    private lazy var mainView : UIView = {
        let v = UIView()
        
        return v
    }()
    
    private lazy var cardView : XQLargeCardView = {
        let v = XQLargeCardView()
        v.applyHandle = {[weak self] in
            guard let srongify = self else {return}
            srongify.applyHandle?()
        }
        return v
    }()
    
    private lazy var chooseUsImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_choose_us")
        return imageView
    }()
    
    private lazy var agreementImageView : UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "home_agreement")
        v.isUserInteractionEnabled = true
        v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(agreeClick)))
        return v
    }()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .clear
        
        self.makeUI();
        self.makeConstraints()
    }
    
    func makeUI() {
        self.view.addSubview(self.scrollView)
        self.scrollView.addSubview(self.mainView)
        self.mainView.addSubview(self.cardView)
        self.mainView.addSubview(self.chooseUsImageView)
        self.mainView.addSubview(self.agreementImageView)
    }
    
    func makeConstraints() {
        
        self.scrollView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.right.equalTo(0)
            make.bottom.equalTo(-UIDevice.xq_bottomSafeMargin() - 48)
        }
        
        self.mainView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(UIDevice.xq_width())
        }
        
        self.cardView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.right.equalTo(0)
            make.height.equalTo(572)
        }
        
        self.chooseUsImageView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(self.cardView.snp.bottom).offset(16)
            make.height.equalTo(395)
        }
        
        self.agreementImageView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(self.chooseUsImageView.snp.bottom).offset(14)
            make.height.equalTo(105)
            make.bottom.equalTo(-16)
        }
    }
    
    override func reloadData() {
        self.cardView.topCardInfo = self.topCardInfo
    }
    
    func endRefreshing() {
        self.scrollView.mj_header?.endRefreshing()
    }
    
    @objc func agreeClick() {
        if (XQInitManager.shared.isLogin()) {
            guard let url = self.repeatableModel?.respondents else {return}
            
            if !url.isEmpty {
                XQRouteTool.jumpController(controller: self, url: url)
            }
        }else {
            XQRouteTool.jumpLogin()
        }
    }
}
